﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Churchtoolkit.Utils.RulesManager;
using System.Linq.Expressions;
using Microsoft.Practices.ServiceLocation;
using Churchtoolkit.Utils.Authentication;

namespace Churchtoolkit
{
    public static class RulesManagerExtention
    {
        public static void RegisterNowRule<TEntity>(this IRulesManager rulesManager,EntityState state, Expression<Func<TEntity,DateTime>> action)
        {
            MemberExpression expression =(MemberExpression) action.Body;
            string dateTimeProperty = expression.Member.Name;
            //----------------------------------------------------------------------------
            rulesManager.Register<TEntity>(state, entity => { entity.GetType().SetPropertyValue(dateTimeProperty, entity, SystemTime.Now()); });
        }

        public static void RegisterCurrentUserRule<TEntity>(this IRulesManager rulesManager, EntityState state,Expression<Func<TEntity, int>> action)
        {
            MemberExpression expression = (MemberExpression)action.Body;
            string dateTimeProperty = expression.Member.Name;
            //----------------------------------------------------------------------------
            rulesManager.Register<TEntity>(state, entity =>
            {
                entity.GetType().SetPropertyValue(dateTimeProperty, entity,
                    ServiceLocator.Current.GetInstance<IAuthentication>().CurrentLogedInMember.Id);
            });
        }
    }
}
